﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AopFilters
{
    internal static class ContextHelper
    {
        /// <summary>
        /// 是否忽略该过滤器
        /// </summary>
        /// <param name="context">过滤器上下文</param>
        /// <param name="filterType">过滤器类型</param>
        public static bool IfIgnoreFilter(this ActionContext context, Type filterType)
        {
            // context.ActionDescriptor.EndpointMetadata 当前方法上或者控制器上全部的特性
            var a = context.ActionDescriptor.EndpointMetadata;
            IgnoreFiltersAttribute? attribute = context.ActionDescriptor.EndpointMetadata.FirstOrDefault(x => x.GetType().Equals(typeof(IgnoreFiltersAttribute))) as IgnoreFiltersAttribute;
            if (attribute == null)
                return false;
            if (attribute.IgnoreFilters == null || attribute.IgnoreFilters.Length == 0)
                return true;
            return attribute.IgnoreFilters.Any(x => x.Equals(filterType));
        }
    }
}
